package com.open.center.common.databind.desensitization;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

/**
 * 脱敏器工厂
 *
 * @author Riche's
 * @since 2025/5/23
 */
public class DesensitizationFactory {

    private DesensitizationFactory() {

    }

    private static final Map<Class<?>, Desensitization<?>> map = new HashMap<>();

    public static Desensitization<?> getDesensitization(Class<?> clazz) {
        if (clazz.isInterface()) {
            throw new UnsupportedOperationException("Desensitization is interface, what is expected is an implementation class !");
        }
        return map.computeIfAbsent(clazz, k -> {
            try {
                return (Desensitization<?>) clazz.getDeclaredConstructor().newInstance();
            } catch (InstantiationException
                     | IllegalAccessException
                     | NoSuchMethodException
                     | InvocationTargetException e) {
                throw new UnsupportedOperationException(e.getMessage(), e);
            }
        });
    }
}
